﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YFrameWork.RunTime;

namespace YFrameWork.RunTime
{
    public static class UIFormExtension
    {
        public static void DelayedShow(this UIForm uIForm)
        {
            if (uIForm.UIDelayedOpen == null)
            {
                return;
            }
            foreach (var item in uIForm.UIDelayedOpen)
            {
                item.Key.SetActive(false);
                GameEntry.Instance.GetComponent<UIComponent>().DelayedShow(item.Key, item.Value);
            }
        }
    }
    /// <summary>
    /// 界面基类
    /// </summary>
    public class UIForm: Sirenix.OdinInspector.SerializedMonoBehaviour, IUIForm
    {
        public Dictionary<GameObject, float> UIDelayedOpen;
        private bool mOpen =false;//界面开启状态
        private CanvasGroup mCanvasGroup;
        protected UIOpenAnimationBase mUIOpenAnim ;
        protected UICloseAnimationBase mUICloseAnim;

        public bool MOpen { get => mOpen; }

        public virtual void Init(object Obj,bool Show = false)
        {
            mCanvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
            if (!Show)
            {
                mCanvasGroup.alpha = 0;
                mCanvasGroup.interactable = false;
                mCanvasGroup.blocksRaycasts = false;
                mOpen = false;
            }
        }
        public virtual void Open(object obj)
        {
      
            if (mOpen) return;
            this.DelayedShow();
            mCanvasGroup.alpha = 1;
            mCanvasGroup.interactable = true;
            mCanvasGroup.blocksRaycasts = true;
            ChangeLanguage();
            if (mUIOpenAnim != null)
            {
                mUIOpenAnim.Open();
            }
            mOpen = true;


        }

        public virtual void Close(object obj)
        {
            if (!mOpen) return;

            if (mUICloseAnim!=null)
            {
                mUICloseAnim.Close(() =>
                {
                    mCanvasGroup.alpha = 0;
                    mCanvasGroup.interactable = false;
                    mCanvasGroup.blocksRaycasts = false;
                    mOpen = false;
                });
            }
            else
            {
                mCanvasGroup.alpha = 0;
                mCanvasGroup.interactable = false;
                mCanvasGroup.blocksRaycasts = false;
                mOpen = false;
            }
        }


        public void ChangeLanguage()
        {
            ILanguage[] languages = this.GetComponentsInChildren<ILanguage>();
            foreach (var item in languages)
            {
                item.SetLanguage();
            }
        }
    }
}